package com.yy.controller.user;

import com.yy.controller.service.AddressBookService;
import com.yy.controller.service.OrderService;
import com.yy.entity.AddressBook;
import com.yy.entity.Orders;
import com.yy.result.Result;
import com.yy.vo.AddressBookVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("userOrderController")
@RequestMapping("/user/order")
@Slf4j
@Api(tags = "客户端订单接口")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("/create")
    @CrossOrigin
    @ApiOperation("创建订单")
    public Result list(@RequestBody Orders orders) {
        Long id = orderService.create(orders);
        return Result.success(id);
    }

    @GetMapping("/getOne/{id}")
    @CrossOrigin
    @ApiOperation("获取订单信息及订单详情")
    public Result getOne(@PathVariable Long id) {
        Orders orders = orderService.getOne(id);
        return Result.success(orders);
    }

    @PostMapping("/overtimeCancel/{id}")
    @CrossOrigin
    @ApiOperation("超时取消订单")
    public Result overtimeCancel(@PathVariable Long id) {
        orderService.overtimeCancel(id);
        return Result.success();
    }

    @GetMapping("/list")
    @CrossOrigin
    @ApiOperation("获取订单列表")
    public Result list() {
        List<Orders> ordersList = orderService.getListByUserId();
        return Result.success(ordersList);
    }

    @PostMapping("/paySuccess/{id}/{payMethod}")
    @CrossOrigin
    @ApiOperation("支付成功")
    public Result paySuccess(@PathVariable Long id, @PathVariable Integer payMethod) {
        orderService.paySuccess(id, payMethod);
        return Result.success();
    }

    @PostMapping("/userCancel/{id}/{cancelReason}")
    @CrossOrigin
    @ApiOperation("用户取消订单")
    public Result userCancel(@PathVariable Long id, @PathVariable String cancelReason) {
        orderService.userCancel(id, cancelReason);
        return Result.success();
    }
    }
